package com.ating.demo.controller;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.ating.demo.Entity.Admin;
import com.ating.demo.Entity.Dto;
import com.ating.demo.Entity.Employee;
import com.ating.demo.Service.AdminService;
import com.ating.demo.Service.ArticleService;
import com.ating.demo.Service.EmployeeService;
import com.ating.demo.common.Result;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

@RestController
public class WebController {

    @Resource
    private EmployeeService employeeService;
    @Resource
    private AdminService adminService;
    @Resource
    private ArticleService articleService;

    @GetMapping("/hello")
    public String Hello(){
        return "111";
    }

    /**登录**/
    @PostMapping("/login")
    public Result login(@RequestBody Dto dto){
        Dto result = null;
        if("ADMIN".equals(dto.getRole())){
             result = adminService.login(dto);
             return Result.success(result);
        } else {
            result = employeeService.login(dto);
            return Result.success(result);
        }
    }

    /**员工注册**/
    @PostMapping("/register")
    public Result register(@RequestBody Employee employee){
        employeeService.register(employee);
        return Result.success();
    }

    /**发送柱状图信息**/
    @GetMapping("/getBar")
    public Result getBarData(){
        Map<String,Object> map = new HashMap<>();
        //获取所有数据
        List<Employee> employeeList = employeeService.selectAll(null);
        Set<String> departmentSet = employeeList.stream().map(Employee::getDepartmentName).collect(Collectors.toSet());
        map.put("department",departmentSet);
        List<Long> countList = new ArrayList<>();
        //统计部门下的员工
        for (String departmentName : departmentSet) {
            long count = employeeList.stream().filter(employee -> employee.getDepartmentName().equals(departmentName)).count();
            countList.add(count);
        }
        map.put("count",countList);
        return Result.success(map);
    }

    @GetMapping("/lineData")
    public Result getLineData(){
        Map<String,Object> map = new HashMap<>();
        Date date = new Date();
        //起始时间
        DateTime start = DateUtil.offsetDay(date,-7);
        List<DateTime> dateTimeList = DateUtil.rangeToList(start,date,DateField.DAY_OF_YEAR);
        //转换时间类型
        List<String> dateStrList = dateTimeList.stream().map(dateTime -> DateUtil.format(dateTime,"MM-dd"))
                .sorted(Comparator.naturalOrder()).collect(Collectors.toList());
        map.put("date",dateStrList);

        List<Integer> countList = new ArrayList<>();
        for (String day : dateStrList) {
            Integer count = articleService.selectCountByDate(day);
            countList.add(count);
        }
        map.put("count",countList);
        return Result.success(map);
    }

    @GetMapping("/pieData")
    public Result getPieData(){
        List<Map<String,Object>> list = new ArrayList<>();
        //获取所有数据
        List<Employee> employeeList = employeeService.selectAll(null);
        Set<String> departmentSet = employeeList.stream().map(Employee::getDepartmentName).collect(Collectors.toSet());
        //统计部门下的员工
        for (String departmentName : departmentSet) {
            Map<String,Object> map = new HashMap<>();
            map.put("name",departmentName);

            long count = employeeList.stream().filter(employee -> employee.getDepartmentName().equals(departmentName)).count();
            map.put("value",count);
            list.add(map);
        }
        return Result.success(list);
    }
}
